package top.ddandang.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import top.ddandang.blog.model.pojo.Tags;
import top.ddandang.blog.mapper.TagsMapper;
import top.ddandang.blog.model.query.PageBean;
import top.ddandang.blog.service.TagsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author D
 * @since 2020-06-23
 */
@Service
public class TagsServiceImpl extends ServiceImpl<TagsMapper, Tags> implements TagsService {

    @Resource
    private TagsMapper tagsMapper;


    @Override
    public void insertTags(String name, String description) {
        Tags tags = new Tags()
                .setName(name)
                .setDescription(description);
        while (true) {
            if(tagsMapper.insert(tags) == 1) {
                break;
            }
        }
    }

    @Override
    public List<Tags> listTags(PageBean pageBean) {
        PageHelper.startPage(pageBean.getPageNo(), pageBean.getPageSize());
        return tagsMapper.selectList(null);
    }

    @Override
    public List<Tags> allTags() {
        return tagsMapper.selectList(null);
    }

    @Override
    public int updateTags(Integer id, String name, String description) {

        UpdateWrapper<Tags> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id)
                .set("name", name)
                .set("description", description);
        return tagsMapper.update(null, updateWrapper);
    }

    @Async
    @Override
    public void updateArticleAmount(Integer[] tagsId, Boolean add) {
        for (Integer id : tagsId) {
            Tags tags = tagsMapper.selectById(id);

            int amount = tags.getArticleAmount();
            if (add) {
                amount++;
            }else {
                amount--;
            }
            tagsMapper.update(null, new UpdateWrapper<Tags>()
            .eq("id", id)
            .set("article_amount", amount));
        }
    }

    @Async
    @Override
    public void updateArticleAmount(Integer tagsId, Boolean add) {

        Tags tags = tagsMapper.selectById(tagsId);
        int amount = tags.getArticleAmount();
        if (add) {
            amount++;
        }else {
            amount--;
        }
        tagsMapper.update(null, new UpdateWrapper<Tags>()
                .eq("id", tagsId)
                .set("article_amount", amount));
    }

    @Async
    @Override
    public void addArticleAmount(Map<Integer, Integer> map) {
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            Tags tags = tagsMapper.selectById(entry.getKey());
            int amount = tags.getArticleAmount() + entry.getValue();
            tagsMapper.update(null, new UpdateWrapper<Tags>()
                    .eq("id", entry.getKey())
                    .set("article_amount", amount));
        }
    }
}
